/**
 * Created by leo on 15/6/3.
 */
(function () {
    "use strict";
    angular.module('Shortcut', [])
        .factory('ShortcutService', function (urlPrefix, $http, Promise) {
            var shortcuts, listeners = [];
            $http.get(urlPrefix + '/userShortcuts.do', {
                cache: true
            }).success(function (data) {
                shortcuts = data;
            });
            return {
                getShortcuts: function () {
                    return Promise(function (resolve, reject) {
                        if (angular.isArray(shortcuts)) {
                            resolve(angular.copy(shortcuts));
                            return;
                        }
                        $http.get(urlPrefix + '/userShortcuts.do', {
                            cache: true
                        }).success(function (data) {
                            shortcuts = data;
                            resolve(angular.copy(shortcuts));
                        }).error(function (data) {
                            reject(data);
                        });
                    });
                },
                addShortcut: function (shortcut) {
                    $http.post(urlPrefix + '/addShortcut.do', '', {params: {shortcut: shortcut}});
                    if (angular.isArray(shortcuts)) {
                        for (var i = 0; i < shortcuts.length; i++) {
                            if (shortcuts[i].shortcutPath == shortcut.shortcutPath) {
                                return;
                            }
                        }
                        shortcuts.push(shortcut);
                        shortcuts[shortcut.shortcutPath] = shortcut;
                        angular.forEach(listeners, function (listener) {
                            listener.call(angular.copy(shortcuts), 'add', angular.copy(shortcut));
                        });
                    }
                },
                removeShortcut: function (shortcutPath) {
                    $http.post(urlPrefix + '/removeShortcut.do', '', {params: {shortcutPath: shortcutPath}});
                    if (angular.isArray(shortcuts)) {
                        for (var i = 0; i < shortcuts.length; i++) {
                            var shortcut = shortcuts[i];
                            if (shortcut.shortcutPath == shortcutPath) {
                                shortcuts.splice(i, 1);
                                angular.forEach(listeners, function (listener) {
                                    listener.call(angular.copy(shortcuts), 'remove', angular.copy(shortcut));
                                });
                                return;
                            }
                        }
                    }
                },
                addChangeListener: function (listener) {
                    listeners.push(listener);
                },
                removeChangeListener: function (listener) {
                    for (var i = 0; i < listeners.length; i++) {
                        if (listener == listeners[i]) {
                            listeners.splice(i, 1);
                            return;
                        }
                    }
                }
            }
        });
}());